package w4156.idog.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateful;
import w4156.idog.model.Card;
import w4156.idog.model.User;
import w4156.idog.model.User.statusType;

/**
 * Session Bean implementation class PlayerSessionBean
 */
@Stateful(mappedName = "ejb/eBlackjack/PlayerSLSB")
public class PlayerSessionBean extends UserSessionBean implements
		PlayerSessionBeanRemote {

	private List<Card> mycards = null;
	private double bidamount = 0;
	/**
	 * @see UserSessionBean#UserSessionBean()
	 */
	public PlayerSessionBean() {
		super();
		init();
	}

	private void init() {
		mycards = new ArrayList<Card>();
	}

	@Override
	public void bidOp(double money) {
		getUser().setMoney(getUser().getMoney()-money);
		bidamount = money;
	}
	
	@Override
	public void doubleOp() {
		getUser().setMoney(getUser().getMoney()-bidamount);
		bidamount = bidamount*2;
	}
	
	@Override
	public List<Card> getMycards() {
		return mycards;
	}

	public void setMycards(List<Card> mycards) {
		this.mycards = mycards;
	}

	@Override
	public double getBidamount() {
		return bidamount;
	}

	@Override
	public void setBidamount(double bidamount) {
		this.bidamount = bidamount;
	}

	@Override
	public Card hitOp() {
		// TODO Auto-generated method stub
		Card card = new Card("", "");
		return card;
	}

	@Override
	public void quit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void standOp() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getcard(Card newcard) {
		// TODO Auto-generated method stub
		mycards.add(newcard);
		int i=0;
	}
	
	public int point(){
		String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
				 "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; 
		Iterator<Card> it=mycards.iterator();
		int res = 0;
    	
    	while(it.hasNext()){
    		String numstr = it.next().getFace();
    		for (int i=0;i<13;i++) {
    			if (numstr.compareToIgnoreCase(faces[i])==0){
    				switch(i)   {
    				case 1:
					case 2:
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
					case 8:
					case 9: res += i+1;
					break;
					case 10:
					case 11:
					case 12:res += 10;
					break;
					case 0: res += 11;
							if (res>21)
								res -= 10;
					break;
					default:
					break;
					}
    				break;
    			}
    		}
    	}
		return res;
	}

	@Override
	public void setwin() {
		// TODO Auto-generated method stub
		getUser().setMoney(getUser().getMoney()+bidamount*2);
		getUser().setStatus(statusType.Win);
	}
}


